﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Collections;
using System.Reflection;

namespace Web.App_Codes
{
    public class JsonHelper
    {
        /// <summary>   
        /// List转成json    
        /// </summary>   
        /// <typeparam name="T"></typeparam>   
        /// <param name="jsonName"></param>   
        /// <param name="list"></param>   
        /// <returns></returns>   
        public static string ListToJson(IList list)
        {
            StringBuilder Json = new StringBuilder();
            if (list.Count > 0)
            {
                string jsonName = list[0].GetType().Name;
                Json.Append("{\"" + jsonName + "\":[");
                for (int i = 0; i < list.Count; i++)
                {
                    PropertyInfo[] pi = list[i].GetType().GetProperties();
                    Json.Append("{");
                    for (int j = 0; j < pi.Length; j++)
                    {
                        object obj = pi[j].GetValue(list[i], null);
                        if (obj != null)
                        {
                            Type type = obj.GetType();
                            if (type.Name.Equals("List`1"))
                            {
                                Json.Append("\"" + pi[j].Name + "\":\"");
                                IList _list = obj as IList;
                                for (int k = 0; k < _list.Count; k++)
                                {
                                    string _str = _list[k].ToString();
                                    if (k == _list.Count - 1)
                                    {
                                        Json.Append(_str + "\"");
                                    }
                                    else
                                    {
                                        Json.Append(_str + ",");
                                    }

                                }
                                //最后一个集合
                                if (j == pi.Length - 1)
                                {
                                    Json.Append("");
                                }
                                else
                                {
                                    Json.Append(",");
                                }

                            }
                            else
                            {
                                Json.Append("\"" + pi[j].Name.ToString() + "\":" + StringFormat(pi[j].GetValue(list[i], null).ToString(), type));

                                if (j < pi.Length - 1)
                                {
                                    Json.Append(",");
                                }
                            }
                        }
                    }
                    Json.Append("}");
                    if (i < list.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
                Json.Append("]}");
            }

            return Json.ToString();
        }

        /// <summary>   
        /// 格式化字符型、日期型、布尔型   
        /// </summary>   
        /// <param name="str"></param>   
        /// <param name="type"></param>   
        /// <returns></returns>   
        private static string StringFormat(string str, Type type)
        {
            if (type == typeof(string))
            {
                str = String2Json(str);
                str = "\"" + str + "\"";
            }
            else if (type == typeof(DateTime))
            {
                str = "\"" + str + "\"";
            }
            else if (type == typeof(bool))
            {
                str = str.ToLower();
            }
            return str;
        }

        /// <summary>   
        /// 过滤特殊字符   
        /// </summary>   
        /// <param name="s"></param>   
        /// <returns></returns>   
        private static string String2Json(String s)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                char c = s.ToCharArray()[i];
                switch (c)
                {
                    case '\"':
                        sb.Append("\\\""); break;
                    case '\\':
                        sb.Append("\\\\"); break;
                    case '/':
                        sb.Append("\\/"); break;
                    case '\b':
                        sb.Append("\\b"); break;
                    case '\f':
                        sb.Append("\\f"); break;
                    case '\n':
                        sb.Append("\\n"); break;
                    case '\r':
                        sb.Append("\\r"); break;
                    case '\t':
                        sb.Append("\\t"); break;
                    default:
                        sb.Append(c); break;
                }
            }
            return sb.ToString();
        }
    }
}